// src/api/system.ts
import http from './http';
import { requestWithRetry } from '@/utils/retry';

// 系统管理API
export const systemAPI = {
  // 用户管理
  getUsers: async (params: any) => {
    try {
      // 使用重试机制获取用户列表
      const response = await requestWithRetry(
        () => http.get('/system/users', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取用户列表失败'
      };
    }
  },
  
  getSalesmen: async () => {
    try {
      // 获取所有销售人员（这里简化处理，实际应该有过滤条件）
      // 使用重试机制获取销售人员列表
      const response = await requestWithRetry(
        () => http.get('/system/users', { 
          params: { 
            role: 'user', // 假设普通用户就是销售人员
            status: 'active' 
          } 
        }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取销售人员列表失败'
      };
    }
  },

  getUser: async (id: string) => {
    try {
      // 使用重试机制获取用户详情
      const response = await requestWithRetry(
        () => http.get(`/system/users/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取用户详情失败'
      };
    }
  },

  createUser: async (data: any) => {
    try {
      const result = await http.post('/system/users', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '创建用户失败'
      };
    }
  },

  updateUser: async (id: string, data: any) => {
    try {
      const result = await http.put(`/system/users/${id}`, data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新用户失败'
      };
    }
  },

  deleteUser: async (id: string) => {
    try {
      const result = await http.delete(`/system/users/${id}`);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除用户失败'
      };
    }
  },

  batchDeleteUsers: async (ids: string[]) => {
    try {
      const result = await http.post('/system/users/batch-delete', { ids });
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '批量删除用户失败'
      };
    }
  },

  // 角色管理
  getRoles: async () => {
    try {
      // 使用重试机制获取角色列表
      const response = await requestWithRetry(
        () => http.get('/system/roles'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取角色列表失败'
      };
    }
  },

  getRole: async (id: string) => {
    try {
      // 使用重试机制获取角色详情
      const response = await requestWithRetry(
        () => http.get(`/system/roles/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取角色详情失败'
      };
    }
  },

  createRole: async (data: any) => {
    try {
      const result = await http.post('/system/roles', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '创建角色失败'
      };
    }
  },

  updateRole: async (id: string, data: any) => {
    try {
      const result = await http.put(`/system/roles/${id}`, data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新角色失败'
      };
    }
  },

  deleteRole: async (id: string) => {
    try {
      const result = await http.delete(`/system/roles/${id}`);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除角色失败'
      };
    }
  },

  // 操作日志
  getOperationLogs: async (params: any) => {
    try {
      // 使用重试机制获取操作日志列表
      const response = await requestWithRetry(
        () => http.get('/system/logs', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取操作日志列表失败'
      };
    }
  },

  getOperationLog: async (id: string) => {
    try {
      // 使用重试机制获取操作日志详情
      const response = await requestWithRetry(
        () => http.get(`/system/logs/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取操作日志详情失败'
      };
    }
  },

  deleteOperationLog: async (id: string) => {
    try {
      const result = await http.delete(`/system/logs/${id}`);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除操作日志失败'
      };
    }
  },

  // 系统设置
  getSystemSettings: async () => {
    try {
      // 使用重试机制获取系统设置
      const response = await requestWithRetry(
        () => http.get('/system/settings'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取系统设置失败'
      };
    }
  },

  updateSystemSettings: async (data: any) => {
    try {
      const result = await http.put('/system/settings', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新系统设置失败'
      };
    }
  },

  // 快捷方式
  getUserShortcuts: async () => {
    try {
      // 使用重试机制获取用户快捷方式
      const response = await requestWithRetry(
        () => http.get('/system/shortcuts'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取用户快捷方式失败'
      };
    }
  },

  updateUserShortcuts: async (data: any) => {
    try {
      const result = await http.put('/system/shortcuts', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新用户快捷方式失败'
      };
    }
  },

  // 用户分组
  getUserGroups: async (params: any) => {
    try {
      // 使用重试机制获取用户分组列表
      const response = await requestWithRetry(
        () => http.get('/system/user-groups', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取用户分组列表失败'
      };
    }
  },

  createUserGroup: async (data: any) => {
    try {
      const result = await http.post('/system/user-groups', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '创建用户分组失败'
      };
    }
  },

  updateUserGroup: async (id: string, data: any) => {
    try {
      const result = await http.put(`/system/user-groups/${id}`, data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新用户分组失败'
      };
    }
  },

  deleteUserGroup: async (id: string) => {
    try {
      const result = await http.delete(`/system/user-groups/${id}`);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除用户分组失败'
      };
    }
  },

  getGroupMembers: async (params: any) => {
    try {
      // 使用重试机制获取分组成员列表
      const response = await requestWithRetry(
        () => http.get('/system/user-groups/members', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取分组成员列表失败'
      };
    }
  },

  addUserToGroup: async (data: any) => {
    try {
      const result = await http.post('/system/user-groups/add-user', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '添加用户到分组失败'
      };
    }
  },

  removeUserFromGroup: async (data: any) => {
    try {
      const result = await http.post('/system/user-groups/remove-user', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '从分组移除用户失败'
      };
    }
  },

  // UI配置
  getUIConfig: async () => {
    try {
      // 使用重试机制获取UI配置
      const response = await requestWithRetry(
        () => http.get('/system/ui-config'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取UI配置失败'
      };
    }
  },

  saveUIConfig: async (data: any) => {
    try {
      const result = await http.post('/system/ui-config', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '保存UI配置失败'
      };
    }
  },

  deleteUIConfig: async () => {
    try {
      const result = await http.delete('/system/ui-config');
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除UI配置失败'
      };
    }
  },

  // 角色权限管理
  getRolePermissions: async (roleId: string) => {
    try {
      const response = await requestWithRetry(
        () => http.get(`/system/roles/${roleId}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取角色权限失败'
      };
    }
  },

  updateRolePermissions: async (roleId: string, permissions: any) => {
    try {
      const result = await http.put(`/system/roles/${roleId}`, { permissions });
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新角色权限失败'
      };
    }
  }
};